package cn.hls.web.controller.dict;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import cn.hls.entity.GoodsCategory;
import cn.hls.entity.vo.Msg;
import cn.hls.entity.vo.PageInfo;
import cn.hls.service.GoodsCategoryService;
import cn.hls.web.controller.base.BaseController;

/**
 *@fileName: GoodsCategoryController.java
 *@package: cn.christ.web.controller.biz
 *@Description:  货物类型Controller
 *@author: yinghui.zhang
 *@email: uuhui@163.com
 *@date: 2015-8-13
 */
@Controller
@RequestMapping("/goodsCategory")
public class GoodsCategoryController extends BaseController<GoodsCategory> {
	@Resource(name = "goodsCategoryService")
	private GoodsCategoryService service;

	/**
	 * 页面跳转
	 * 
	 * @return ModelAndView
	 * @throws Exception
	 */
	@RequestMapping("/view")
	public ModelAndView view() throws Exception {
		return new ModelAndView("dict/goodsCategory");
	}

	/**
	 * 新增操作
	 * 
	 * @param entity
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/add")
	@ResponseBody
	public Msg add(GoodsCategory entity) throws Exception {
		GoodsCategory tempEntity = service.loadGoodsCategoryByCodeOrName(entity.getCode().trim(), entity.getName().trim());
		if (tempEntity != null) {
			msg.setInfo("货物类型名称或编码已经存在");
			msg.setStatus(201);
		} else {
			boolean flag = service.add(entity);
			if (flag) {
				msg.setInfo("操作成功");
				msg.setStatus(200);
			} else {
				msg.setInfo("操作失败");
				msg.setStatus(200);
			}
		}
		return msg;
	}

	/** 删除操作 **/
	@RequestMapping("/del")
	@ResponseBody
	public Msg del(Long id) throws Exception {
		try {
			service.delete(id);
			msg.setInfo("删除成功");
			msg.setStatus(200);
			return msg;
		} catch (Exception ex) {
			throw ex;
		}
	}

	/**
	 * 分页查询
	 * 
	 * @param page
	 *            分页对象
	 * @param entity
	 *            查询实时对象
	 * @return PageInfo<payTeam>
	 * @throws Exception
	 */
	@RequestMapping("/list")
	@ResponseBody
	public PageInfo<GoodsCategory> list(PageInfo<GoodsCategory> page, GoodsCategory entity)
			throws Exception {
		return service.loadGoodsCategoryInfo(page, entity);
	}
	
	@RequestMapping("/loadGoodsCategory")
	@ResponseBody
	public List<GoodsCategory>loadGoodsCategory(GoodsCategory entity)throws Exception{
		return service.loadGoodsCategoryInfo(entity);
	}

	/**
	 * 更新操作
	 * 
	 * @param entity
	 *           货物类型对象
	 * @return Msg
	 * @throws Exception
	 */
	@RequestMapping("/update")
	@ResponseBody
	public Msg update(GoodsCategory entity) throws Exception {
		boolean flag = service.update(entity);
		if (flag) {
			msg.setInfo("操作成功");
			msg.setStatus(200);
		} else {
			msg.setInfo("操作失败");
			msg.setStatus(200);
		}
		return msg;
	}
	@RequestMapping("/loadGoodsCategoryInfo")
	@ResponseBody
	public List<GoodsCategory> loadGoodsCategoryInfo(GoodsCategory entity)throws Exception{
		return service.loadGoodsCategoryInfo( entity);
	}
	
}
